import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { findBanner } from '@/api/home'
import { findTopCategory } from '@/api/category'
export function useGetImg () {
  const listRef = ref([])
  const categoryList = ref([])
  const categroyName = ref('')

  // 初始化调方法获取图片数据
  onMounted(() => {
    getList()
    getCategoryList()
  })

  // onBeforeRouteUpdate((to) => {
  //   getCategoryList(to.params.id)
  // })

  // 获取图片数据的方法
  async function getList () {
    // 掉接口获取数据赋值给本地
    const res = await findBanner()
    listRef.value = res.result
  }

  const route = useRoute()
  // 获取分类数据的方法
  async function getCategoryList (id = route.params.id) {
    const res = await findTopCategory(id)
    categoryList.value = res.result
    categroyName.value = res.result.name
  }
  return {
    listRef,
    categoryList,
    categroyName
  }
}
